package com.sinven.service;

import com.sinven.common.ActionResult;
import com.sinven.common.Pager;
import com.sinven.mapper.StudentMapper;
import com.sinven.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public ActionResult<List<Student>> queryStudent(Integer page, int pageSize) {
        ActionResult<List<Student>> actionResult = new ActionResult<>();
        //得到分类文章数目
        long count = studentMapper.countStudent();
        if (count == 0) {
            actionResult.setCode(500);
            actionResult.setMsg("此分类无文章");
            return actionResult;
        }
        //得到分类下文章
        int startIndex = 0;
        if (page != null || page > 0) {
            startIndex = (page - 1) * pageSize;
        }
        List<Student> students = studentMapper.queryStudents(startIndex, pageSize);

        actionResult.setCode(0);
        actionResult.setCount(count);
        actionResult.setData(students);
        return actionResult;
    }

    @Override
    public boolean updateStudent(Student student) {
        int rows = studentMapper.updateStudent(student);
        
        return rows>0;
    }

    @Override
    public boolean addStudent(Student student) {
        int rows = studentMapper.addStudent(student);
       
        return rows>0;
    }

    @Override
    public boolean deleteStudent(long sid) {
        int rows = studentMapper.deleteStudent(sid);
       return rows>0;
    }

    @Override
    public Student queryStudent(long sid) {
        return studentMapper.queryStudent(sid);
    }

    @Override
    public Pager<Student> queryStudents(Long curPage) {
        //得到分类文章数目
        long totalRecords = studentMapper.countStudent();
        if(totalRecords<=0){
            return null;
        }
        Pager<Student> pager = new Pager<>(totalRecords,curPage,12);
        List<Student> students = studentMapper.queryStudents(pager.getStartIndex(), pager.getPageSize());
        pager.setDatas(students);
        return pager;
    }


}
